<template>
  <div id="ChartsWrap"></div>
</template>

<script>
export default {
  name: "EchartWrap",
  data() {
    return {
      legendText: ['PM2.5', 'PM10'],
      xAxisDate: ["00:00"],
      seriesData: [
        { name: 'PM2.5', type: 'line', stack: '总量', data: [0] },
        { name: 'PM10', type: 'line', stack: '总量', data: [0] },
      ]
    }
  },

  mounted() {
    this.init()
  },

  methods: {
    handleCensusData(data) {
      if (data.legendText.length == 0 && data.xAxisDate.length == 0 && data.seriesData.length == 0) {
        this.legendText = ['PM2.5', 'PM10'];
        this.xAxisDate = ["00:00"];
        this.seriesData = [
          { name: 'PM2.5', type: 'line', stack: '总量', data: [0] },
          { name: 'PM10', type: 'line', stack: '总量', data: [0] },
        ];
        this.init();
        return
      }
      this.legendText = data.legendText;
      this.xAxisDate = data.xAxisDate;

      let arr = [];
      data.seriesData.forEach(item => {
        arr.push({
          name: item.name,
          type: 'line',
          stack: '总量',
          data: item.data
        })
      })
      this.seriesData = arr;
      this.init();
    },

    init() {
      var echarts = require('echarts');
      var myChart = echarts.init(document.getElementById('ChartsWrap'));
      let option = {
        color: ['#3fb1e3', '#6be6c1', '#626c91', '#a0a7e6',
          '#c4ebad', '#96dee8'],
        tooltip: {
          trigger: 'axis',
          // formatter: "{a} <br/>{b} : {c}$ ({d}%)",
        },
        legend: {
          data: this.legendText,
          top: '20',
          right: '45',
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: this.xAxisDate,
          axisTick: {
            show: false   //去除刻度线
          },
          axisLine: {
            lineStyle: {
              type: 'solid',
              color: '#ccc',//左边线的颜色
              width: '1'//坐标线的宽度
            }
          },
          axisLabel: {
            textStyle: {
              color: '#ccc',//坐标值得具体的颜色
            }
          }
        },
        yAxis: {
          type: 'value',
          nameTextStyle: {
            color: '#000'
          },
          axisTick: {
            show: false   //去除刻度线
          },
          axisLine: {
            lineStyle: {
              type: 'solid',
              color: '#ccc',//左边线的颜色
              width: '1'//坐标线的宽度
            }
          },
          axisLabel: {
            textStyle: {
              color: '#ccc',//坐标值得具体的颜色
            }
          },
          splitLine: {  //分割线
            lineStyle: {
              type: 'dash',
            }
          }
        },
        series: this.seriesData
      };

      myChart.setOption(option, true);
    },
  }
}
</script>

<style scoped>
#ChartsWrap {
  width: 100%;
  height: 100%;
  padding: 8px 10px 5px;
}
</style>
